/*
 * File Name：TitleBar.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： TitleBar.java
 * Modify By：res-jxliu
 * Modify Date：2015年3月21日
 * Modify Type：Add
 */
package com.ciwong.mobilelib.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.libs.utils.DeviceUtils;
import com.ciwong.mobilelib.R;
import com.ciwong.mobilelib.i.ViewOnClickListener;

/**
 * 标题栏
 *
 * @author res-jxliu
 * @version ciwong v.1.0 2015年3月21日
 * @since ciwong v.1.0
 */
public class TitleBar extends RelativeLayout {
    private TextView goBackTitle;

    private TextView centerTitle;

    private TextView rightText;

    private ImageView img_btn_right;

    private ImageView goBackImg;

    private ProgressBar leftProBar;

    private ProgressBar centerProBar;

    private ProgressBar rightProBar;

    private LinearLayout leftContainer;

    private RelationMenu2 titleSelector;

    private LinearLayout rightContainer;

    private LinearLayout centerContainer;

    private IndicateText rightIndicateText;

    private ViewGroup titleContainer;
    /**
     * titlebar下方的分割线
     */
    View view_titlebar_divider;

    /**
     * Cnstructor Method。
     *
     * @param context  上下文
     * @param attrs    属性
     * @param defStyle 默认样式
     */
    public TitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    /**
     * Cnstructor Method。
     *
     * @param context 上下文环境
     * @param attrs   属性
     */
    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * Cnstructor Method。
     *
     * @param context 上下文环境
     */
    public TitleBar(Context context) {
        super(context);
        init();
    }


    private void init() {
        View.inflate(getContext(), R.layout.title_bar, this);
        goBackTitle = (TextView) findViewById(R.id.go_back_title);
        centerTitle = (TextView) findViewById(R.id.title_center);
        rightText = (TextView) findViewById(R.id.tv_right);
        view_titlebar_divider = findViewById(R.id.view_titlebar_divider);
        img_btn_right = (ImageView) findViewById(R.id.img_btn_right);

        goBackImg = (ImageView) findViewById(R.id.go_back_img);

        leftProBar = (ProgressBar) findViewById(R.id.pro_bar_left);

        centerProBar = (ProgressBar) findViewById(R.id.pro_bar_center);

        rightProBar = (ProgressBar) findViewById(R.id.pro_bar_right);

        leftContainer = (LinearLayout) findViewById(R.id.title_bar_left);

        rightContainer = (LinearLayout) findViewById(R.id.title_bar_right);

        // titleSelector = (RelationMenu2) findViewById(R.id.title_selector);

        centerContainer = (LinearLayout) findViewById(R.id.title_bar_center);

        rightIndicateText = (IndicateText) findViewById(
                R.id.right_indicate_text);
        titleContainer = (ViewGroup) findViewById(R.id.title_bar);
    }

    /**
     * 设置滑动菜单标题
     *
     * @param menuArr 数组
     */
    public void setMenuTitle(String[] menuArr) {
        centerTitle.setVisibility(View.GONE);
        titleSelector.setVisibility(View.VISIBLE);
        titleSelector.setMenuArr(menuArr);
    }

    /**
     * 动态标题
     *
     * @param text
     */
    public void setDynamicTitle(String text) {
        centerTitle.setVisibility(View.VISIBLE);
        titleSelector.setVisibility(View.GONE);
        centerTitle.setText(text);
    }

    /**
     * 隐藏标题
     */
    public void hideTitleBar() {
        centerTitle.setVisibility(View.GONE);
    }

    /**
     * 设置标题
     *
     * @param resid 标题
     */
    public void setTitle(int resid) {
        centerTitle.setText(resid);
    }

    /**
     * 设置标题
     *
     * @param text 标题
     */
    public void setTitle(String text) {
        centerTitle.setText(text);
    }

    public String getTitle() {
        return centerTitle.getText().toString();
    }

    /**
     * 设置标题的字体颜色及大小
     *
     * @param colorId
     * @param size    0为默认
     */
    public void setTitleStyle(int colorId, int size) {
        centerTitle.setTextColor(getResources().getColor(colorId));
        if (size > 0) {
            centerTitle.setTextSize(size);
        }
    }

    public void setTitleBarDividerGone() {
        view_titlebar_divider.setVisibility(View.GONE);
    }

    /**
     * 设置菜单标题的事件监听
     *
     * @param listener 事件监听
     */
    public void setMenuListener(RelationMenu2.OnSelectChangedListener listener) {
        titleSelector.setOnSelectChangedListener(listener);
    }

    /**
     * 隐藏左边标题
     */
    public void hideBackBar() {
        leftContainer.setVisibility(View.GONE);
        goBackTitle.setVisibility(View.GONE);
        goBackImg.setVisibility(View.GONE);
        leftProBar.setVisibility(View.GONE);
    }

    /**
     * 显示返回按钮
     */
    public void showBackBar() {
        leftContainer.setVisibility(View.VISIBLE);
        goBackTitle.setVisibility(View.VISIBLE);
        goBackImg.setVisibility(View.VISIBLE);
    }

    /**
     * 设置返回按钮的文字
     *
     * @param resid 标题
     */
    public void setBackText(int resid) {
        goBackTitle.setVisibility(View.VISIBLE);
        goBackTitle.setText(resid);
    }

    /**
     * 设置返回按钮文字
     *
     * @param text 返回按钮的文字
     */
    public void setBackText(String text) {
        goBackTitle.setVisibility(View.VISIBLE);
        goBackTitle.setText(text);
    }

    /**
     * 设置返回按钮的字体颜色及size
     *
     * @param colorId
     * @param size    0为默认
     */
    public void setBackTextSytle(int colorId, int size) {
        goBackTitle.setTextColor(getResources().getColor(colorId));
        if (size > 0) {
            goBackTitle.setTextSize(size);
        }

    }

    /**
     * 设置点击返回按钮的监听
     *
     * @param listener 返回监听
     */
    public void setBackListener(ViewOnClickListener listener) {
        leftContainer.setOnClickListener(listener);
    }

    /**
     * 显示左边的进度条
     *
     * @param text 显示的文字，null表示不显示文字
     */
    public void showLeftProgressBar(String text) {
        goBackImg.setVisibility(View.GONE);
        leftProBar.setVisibility(View.VISIBLE);
        if (text != null) {
            goBackTitle.setText(text);
        } else {
            goBackTitle.setVisibility(View.GONE);
        }

    }

    /**
     * 隐藏左边的进度条
     */
    public void hideLeftProgressBar() {
        goBackImg.setVisibility(View.VISIBLE);
        goBackTitle.setVisibility(View.VISIBLE);
        leftProBar.setVisibility(View.GONE);
    }

    /**
     * 中间标题事件
     *
     * @param listener 事件监听
     */
    public void setMiddleTextListener(ViewOnClickListener listener) {
        centerContainer.setOnClickListener(listener);

    }

    /**
     * 显示中间的进度条
     *
     * @param text 需要显示的文字,null表示不显示文字
     */
    public void showMiddleProgressBar(String text) {
        centerProBar.setVisibility(View.VISIBLE);
        if (text != null) {
            centerTitle.setText(text);
        } else {
            centerTitle.setVisibility(View.GONE);
        }
    }

    /**
     * 中间的进度条是否显示中
     *
     * @return 是否显示
     */

    public boolean isShowMiddleProgressBar() {
        return centerProBar.getVisibility() == View.VISIBLE;
    }

    /**
     * 隐藏中间的进度条
     */
    public void hideMiddleProgressBar() {
        centerTitle.setVisibility(View.VISIBLE);
        centerProBar.setVisibility(View.GONE);

    }

    /**
     * 显示右边的进度条
     *
     * @param text 需要显示的文字,null表示不显示文字
     */
    public void showRightProgressBar(String text) {
        rightProBar.setVisibility(View.VISIBLE);
        if (text != null) {
            rightText.setText(text);
        } else {
            rightText.setVisibility(View.GONE);
        }
    }

    /**
     * 显示右边按钮
     */
    public void showRightBtn() {
        rightText.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏右边的按钮
     */
    public void hideRightBtn() {
        rightText.setVisibility(View.GONE);
    }

    /**
     * 隐藏右边的按钮
     */
    public void hideRightImgBtn() {
        img_btn_right.setVisibility(View.GONE);
    }

    /**
     * 隐藏右边的进度条
     */
    public void hideRightProgressBar() {
        rightText.setVisibility(View.VISIBLE);
        rightProBar.setVisibility(View.GONE);
    }

    /**
     * 设置右边按钮的文字
     *
     * @param resid 标题
     */
    public void setRightBtnText(int resid) {
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(resid);
    }

    public void setRightBtnTextAndRightDrawable(String text, int rightDrawable) {
        setRightBtnText(text);
        setRightBtnRightDrawable(rightDrawable);

    }

    public void setRightBtnRightDrawable(int rightDrawable) {
        float dimension = getResources().getDimension(R.dimen.dp_8);
        rightText.setCompoundDrawablePadding((int) dimension);
        Drawable drawable = getResources().getDrawable(rightDrawable);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        rightText.setCompoundDrawables(null, null, drawable, null);
    }

    /**
     * 设置右边按钮的文字
     *
     * @param text 标题
     */
    public void setRightBtnText(String text) {
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(text);
    }

    /**
     * 设置右边按钮的文字
     *
     * @param text
     * @param colorRes 字体颜色的资源id
     */
    public void setRightBtnText(String text, int colorRes) {
        rightText.setVisibility(View.VISIBLE);
        rightText.setTextColor(getResources().getColor(colorRes));
        rightText.setText(text);
    }

    /**
     * 设置右边按钮的文字
     *
     * @param spannableString 字体颜色的资源Spanna
     */
    public void setRightSpannaText(SpannableString spannableString) {
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(spannableString);
    }

    /**
     * 设置右边按钮的监听
     *
     * @param listener 按钮的监听事件
     */
    public void setRightBtnListener(ViewOnClickListener listener) {
        rightText.setOnClickListener(listener);
    }

    /**
     * 设置右边按钮的背景
     *
     * @param drawable 背景drawable
     */
    public void setRightBtnBG(Drawable drawable) {
        rightText.setVisibility(View.VISIBLE);
        ViewGroup.LayoutParams params = rightText.getLayoutParams();
        params.width = DeviceUtils.dip2px(25);
        params.height = DeviceUtils.dip2px(25);
        rightText.setLayoutParams(params);
        rightText.setBackgroundDrawable(drawable);
    }

    /**
     * 设置右边按钮的背景
     *
     * @param resId 资源
     */
    public void setRightBtnBG(int resId) {
        rightText.setVisibility(View.VISIBLE);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rightText
                .getLayoutParams();
        params.width = DeviceUtils.dip2px(25);
        params.height = DeviceUtils.dip2px(25);
        params.rightMargin = DeviceUtils.dip2px(10);
        rightText.setLayoutParams(params);
        rightText.setBackgroundResource(resId);
    }


    /**
     * 设置右边按钮的背景
     *
     * @param resId 资源
     */
    public void setImgRightBtn(int resId) {
        img_btn_right.setVisibility(View.VISIBLE);
        img_btn_right.setImageResource(resId);
    }


    /**
     * 设置右边按钮的监听
     *
     * @param listener 按钮的监听事件
     */
    public void setImgRightBtnListener(ViewOnClickListener listener) {
        img_btn_right.setOnClickListener(listener);
    }

    public void setRightBtnEnable(boolean enable) {
        rightText.setClickable(enable);
    }

    /**
     * 获取返回布局ID
     *
     * @return 返回布局ID
     */
    public int getLeftId() {
        return R.id.title_bar_left;
    }

    /**
     * 获取title布局ID
     *
     * @return 返回title布局ID
     */
    public int getCenterId() {
        return R.id.title_bar_center;
    }

    /**
     * 获取右边布局ID
     *
     * @return 返回右边布局ID
     */
    public int getRightId() {
        return R.id.tv_right;
    }

    /**
     * 隐藏右边的提示数字。
     */
    public void hideRightIndicateText() {
        rightIndicateText.setVisibility(View.GONE);
    }

    /**
     * 显示右边的提示数字
     */
    public void showRightIndicateText() {
        rightIndicateText.setVisibility(View.VISIBLE);
    }

    /**
     * 设置右边的提示数字
     *
     * @param conut 数字
     */
    public void setIndicateText(int conut) {
        rightIndicateText.setText("" + conut);
    }

    /**
     * 设置标题栏的背景
     *
     * @param drawable
     */
    public void setTitleBarBg(Drawable drawable) {
        titleContainer.setBackgroundDrawable(drawable);
    }

    /**
     * 设置标题栏的背景
     *
     * @param resId
     */
    public void setTitleBarBg(int resId) {
        titleContainer.setBackgroundResource(resId);
    }

    /**
     * 设置标题栏的背景颜色
     *
     * @param colorId
     */
    public void setTitleBarColor(int colorId) {
        titleContainer.setBackgroundColor(getResources().getColor(colorId));
    }

    public void setBackImg(int id) {
        if (goBackImg != null) {
            goBackImg.setImageResource(id);
        }
    }

    public void showRightTextAndTextDrawable(String text, Drawable leftDrawable) {
        if (text != null) {
            rightText.setText(text);
        } else {
            rightText.setVisibility(View.GONE);
        }
        leftDrawable.setBounds(0, 0, leftDrawable.getIntrinsicWidth(), leftDrawable.getIntrinsicHeight());
        rightText.setCompoundDrawablePadding(DeviceUtils.dip2px(5));
        rightText.setCompoundDrawables(leftDrawable, null, null, null);
    }

    public void setOnCenterTitleClickListener(ViewOnClickListener listener) {
        centerTitle.setOnClickListener(listener);
    }
}
